Java Code Examples for com.hp.hpl.jena.graph.Triple

The following are top voted examples for showing how to use com.hp.hpl.jena.graph.Triple. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: S2X   File: SparkBGP.java   Source Code and License 6 votes vote down vote up
private void buildBGP(OpBGP op) {
final List<Triple> triples = op.getPattern().getList();
for (final Triple triple : triples) {
    final Node subjectNode = triple.getSubject();
    final Node predicateNode = triple.getPredicate();
    final Node objectNode = triple.getObject();

    final String subject = PrefixUtil.collapsePrefix(FmtUtils
	    .stringForNode(subjectNode, prefixes));
    final String object = PrefixUtil.collapsePrefix(FmtUtils
	    .stringForNode(objectNode, prefixes));
    final String predicate = PrefixUtil.collapsePrefix(FmtUtils
	    .stringForNode(predicateNode, prefixes));

    if (bgp == null) {
	bgp = new ArrayList<TriplePattern>();
    }
    bgp.add(new TriplePattern(subject, predicate, object));
}
   }
 
Example 2
Project: S2RDF   File: ReorderNoCross.java   Source Code and License 6 votes vote down vote up
public BasicPattern reorder(BasicPattern pattern) {
    inputPattern = pattern;
    List<Triple> triples = inputPattern.getList();

    int idx = chooseFirst();
    Triple triple = triples.get(idx);
    outputPattern.add(triple);
    joinSchema.addAll(getVarsOfTriple(triple));
    triples.remove(idx);

    while (!triples.isEmpty()) {
        idx = chooseNext();
        triple = triples.get(idx);
        outputPattern.add(triple);
        joinSchema.addAll(getVarsOfTriple(triple));
        triples.remove(idx);
    }

    return outputPattern;
}
 
Example 3
Project: S2RDF   File: TripleGroup.java   Source Code and License 6 votes vote down vote up
private HashMap<String, String[]> getMappingVarsOfTriple(Triple t) {
	HashMap<String, String[]> result = new HashMap<String, String[]>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable())
		result.put(subject.getName(),
				new String[] { Tags.SUBJECT_COLUMN_NAME });
	if (predicate.isVariable()) {
		result.put(predicate.getName(),
				new String[] { Tags.PREDICATE_COLUMN_NAME });
	}
	if (object.isVariable()) {
		result.put(object.getName(),
				new String[] { Tags.OBJECT_COLUMN_NAME });
	}
	return result;
}
 
Example 4
Project: PigSPARQL   File: ReorderNoCross.java   Source Code and License 6 votes vote down vote up
public BasicPattern reorder(BasicPattern pattern) {
	inputPattern = pattern;
	List<Triple> triples = inputPattern.getList();

	int idx = chooseFirst();
	Triple triple = triples.get(idx);
	outputPattern.add(triple);
	addToJoinVars(getVarsOfTriple(triple));
	triples.remove(idx);

	while (!triples.isEmpty()) {
		idx = chooseNext();
		triple = triples.get(idx);
		outputPattern.add(triple);
		addToJoinVars(getVarsOfTriple(triple));		
		triples.remove(idx);
	}

	return outputPattern;
}
 
Example 5
Project: PigSPARQL   File: PigBGP.java   Source Code and License 6 votes vote down vote up
protected ArrayList<String> getSchemaOfTriple(Triple t) {
	ArrayList<String> schema = new ArrayList<String>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable())
		schema.add(subject.getName());
	else
		schema.add(Tags.NO_VAR);
	if (predicate.isVariable())
		schema.add(predicate.getName());
	else
		schema.add(Tags.NO_VAR);
	if (object.isVariable())
		schema.add(object.getName());
	else
		schema.add(Tags.NO_VAR);
	return schema;
}
 
Example 6
Project: Sempala   File: ReorderNoCross.java   Source Code and License 6 votes vote down vote up
public BasicPattern reorder(BasicPattern pattern) {
	inputPattern = pattern;
	List<Triple> triples = inputPattern.getList();

	int idx = chooseFirst();
	Triple triple = triples.get(idx);
	outputPattern.add(triple);
	joinSchema.addAll(getVarsOfTriple(triple));
	triples.remove(idx);

	while (!triples.isEmpty()) {
		idx = chooseNext();
		triple = triples.get(idx);
		outputPattern.add(triple);
		joinSchema.addAll(getVarsOfTriple(triple));
		triples.remove(idx);
	}

	return outputPattern;
}
 
Example 7
Project: Sempala   File: SparkComplexTripleGroup.java   Source Code and License 6 votes vote down vote up
private HashMap<String, String[]> getMappingVarsOfTriple(Triple t) {
	HashMap<String, String[]> result = new HashMap<String, String[]>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable()) {
		result.put(subject.getName(), new String[] { Tags.SUBJECT_COLUMN_NAME });
	}
	if (predicate.isVariable()) {
		selectFromTripleStore = true;
		result.put(predicate.getName(), new String[] { Tags.PREDICATE_COLUMN_NAME });
	}
	if (object.isVariable()) {
		if (selectFromTripleStore) {
			result.put(object.getName(), new String[] { Tags.OBJECT_COLUMN_NAME });
		} else {
			String objectString = object.getName();
			String predicateString = getPropertyFromURI(FmtUtils
					.stringForNode(predicate, prefixMapping), false);
			result.put(objectString, new String[] { SpecialCharFilter.filter(predicateString) });
		}
	}
	return result;
}
 
Example 8
Project: Sempala   File: TripleGroup.java   Source Code and License 6 votes vote down vote up
private HashMap<String, String[]> getMappingVarsOfTriple(Triple t) {
	HashMap<String, String[]> result = new HashMap<String, String[]>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable())
		result.put(subject.getName(),
				new String[] { Tags.SUBJECT_COLUMN_NAME });
	if (predicate.isVariable()) {
		selectFromTripleStore = true;
		result.put(predicate.getName(),
				new String[] { Tags.PREDICATE_COLUMN_NAME });
	}
	if (object.isVariable()) {
		if (selectFromTripleStore) {
			result.put(object.getName(),
					new String[] { Tags.OBJECT_COLUMN_NAME });
		} else {
			result.put(object.getName(), new String[] { SpecialCharFilter
					.filter(FmtUtils
							.stringForNode(predicate, prefixMapping)) });
		}
	}
	return result;
}
 
Example 9
Project: harvesters   File: DataRdfizer.java   Source Code and License 6 votes vote down vote up
private static void creatRDF(String[] line) {
    String s = line[0];
    String p = line[1];
    String o = line[2];

    Node nodeS = Node.createURI(s);
    Node nodeP = Node.createURI("<" + p + ">");
    Node nodeO = null;

    if (o.contains("http")) {
        nodeO = Node.createURI("<" + o + ">");
    } else if (o.contains(".")) {
        //double
        NodeFactoryExtra c = new NodeFactoryExtra();
        nodeO = c.doubleToNode(Double.parseDouble(o));
    } else {
        nodeO = Node.createLiteral(o);
    }
    Triple triple = Triple.create(nodeS, nodeS, nodeS);

    //Statement sta = ResourceFactory.createStatement(s, p, o);
    graph.add(triple);

}
 
Example 10
Project: marklogic-contentpump   File: RDFReader.java   Source Code and License 6 votes vote down vote up
protected boolean nextStreamingTripleKeyValue() throws IOException, InterruptedException {
    if(rdfIter == null) return false;
    setKey();
    write("<sem:triples xmlns:sem='http://marklogic.com/semantics'>");
    int max = MAXTRIPLESPERDOCUMENT;
    while (max > 0 && rdfIter.hasNext()) {
        Triple triple = (Triple) rdfIter.next();
        write("<sem:triple>");
        write(subject(triple.getSubject()));
        write(predicate(triple.getPredicate()));
        write(object(triple.getObject()));
        write("</sem:triple>");
        notifyUser();
        max--;
    }
    write("</sem:triples>\n");

    if (!rdfIter.hasNext()) {
        pos = 1;
    }

    writeValue();
    return true;
}
 
Example 11
Project: r2rml-kit   File: D2RQQueryHandler.java   Source Code and License 6 votes vote down vote up
public BindingQueryPlan prepareBindings(GraphQuery q, Node[] variables) {   
	this.variables = variables;
	this.indexes = new HashMap<Node,Integer>();
	for (int i = 0; i < variables.length; i++) {
		indexes.put(variables[i], new Integer(i));
	}
	BasicPattern pattern = new BasicPattern();
	for (Triple t: q.getPattern()) {
		pattern.add(t);
	}
	Plan plan = QueryEngineD2RQ.getFactory().create(new OpBGP(pattern), dataset, null, null);
	final ExtendedIterator<Domain> queryIterator = new Map1Iterator<Binding,Domain>(new BindingToDomain(), plan.iterator());
	return new BindingQueryPlan() {
		public ExtendedIterator<Domain> executeBindings() {
			return queryIterator;
		}
	};
}
 
Example 12
Project: c4a_data_repository   File: TripleRelation.java   Source Code and License 6 votes vote down vote up
public TripleRelation selectTriple(Triple t) {
	MutableRelation newBase = new MutableRelation(baseRelation());
	NodeMaker s = nodeMaker(SUBJECT).selectNode(t.getSubject(), newBase);
	if (s.equals(NodeMaker.EMPTY)) return null;
	NodeMaker p = nodeMaker(PREDICATE).selectNode(t.getPredicate(), newBase);
	if (p.equals(NodeMaker.EMPTY)) return null;
	NodeMaker o = nodeMaker(OBJECT).selectNode(t.getObject(), newBase);
	if (o.equals(NodeMaker.EMPTY)) return null;
	Set<ProjectionSpec> projections = new HashSet<ProjectionSpec>();
	projections.addAll(s.projectionSpecs());
	projections.addAll(p.projectionSpecs());
	projections.addAll(o.projectionSpecs());
	newBase.project(projections);
	if (!s.projectionSpecs().isEmpty() && o.projectionSpecs().isEmpty()) {
	    newBase.swapLimits();
	}
	return new TripleRelation(newBase.immutableSnapshot(), s, p, o);
}
 
Example 13
Project: c4a_data_repository   File: D2RQQueryHandler.java   Source Code and License 6 votes vote down vote up
public BindingQueryPlan prepareBindings(Query q, Node[] variables) {   
	this.variables = variables;
	this.indexes = new HashMap<Node,Integer>();
	for (int i = 0; i < variables.length; i++) {
		indexes.put(variables[i], new Integer(i));
	}
	BasicPattern pattern = new BasicPattern();
	for (Triple t: q.getPattern()) {
		pattern.add(t);
	}
	Plan plan = QueryEngineD2RQ.getFactory().create(new OpBGP(pattern), dataset, null, null);
	final ExtendedIterator<Domain> queryIterator = new Map1Iterator<Binding,Domain>(new BindingToDomain(), plan.iterator());
	return new BindingQueryPlan() {
		public ExtendedIterator<Domain> executeBindings() {
			return queryIterator;
		}
	};
}
 
Example 14
Project: c4a_data_repository   File: ExprTransformTest2.java   Source Code and License 6 votes vote down vote up
public void testLang()
{
	List<Triple> pattern = new ArrayList<Triple>();
	pattern.add(Triple.create(Node.createVariable("s"), RDFS.label.asNode(), Node.createVariable("o")));
	NodeRelation[] rels = translate(pattern, "optimizer/filtertests.n3");
	
	NodeRelation label_fr_be  = search("table1", "label_fr_be", rels);
	NodeRelation label_en     = search("table1", "label_en", rels);
	NodeRelation label_noLang = search("table1", "label", rels);
	
	Expr filterFR    = new E_Equals(new E_Lang(new ExprVar("o")), NodeValue.makeString("fr"));
	Expr filterEN_TAG_EN = new E_Equals(new E_Lang(new ExprVar("o")), NodeValue.makeNode("en", "en", (String) null));
	Expr filterFR_BE = new E_Equals(new E_Lang(new ExprVar("o")), NodeValue.makeString("fr-BE"));
	Expr filter      = new E_Equals(new E_Lang(new ExprVar("o")), NodeValue.makeString(""));
	
	assertEquals("LANG(label_fr_be) = \"fr\" should be FALSE",   Expression.FALSE, TransformExprToSQLApplyer.convert(filterFR, label_fr_be));
	assertEquals("LANG(label_en) = \"fr\" should be FALSE",      Expression.FALSE, TransformExprToSQLApplyer.convert(filterFR, label_en));
	assertEquals("LANG(label_fr_be) = \"fr_be\" should be TRUE", Expression.TRUE,  TransformExprToSQLApplyer.convert(filterFR_BE, label_fr_be));
	assertEquals("LANG(label_en) = \"en\"@en should be FALSE",   Expression.FALSE, TransformExprToSQLApplyer.convert(filterEN_TAG_EN, label_en));
	assertEquals("LANG(label_noLang) = \"\" should be TRUE",     Expression.TRUE,  TransformExprToSQLApplyer.convert(filter, label_noLang));
}
 
Example 15
Project: c4a_data_repository   File: ExprTransformTest2.java   Source Code and License 6 votes vote down vote up
public void testDataType()
{
	List<Triple> pattern = new ArrayList<Triple>();
	pattern.add(Triple.create(Node.createVariable("s"), Node.createURI("http://example.org/value"), Node.createVariable("o")));
	NodeRelation[] rels = translate(pattern, "optimizer/filtertests.n3");
	
	NodeRelation intvalue = search("table2", "intvalue", rels);
	NodeRelation value = search("table2", "value", rels);
	
	pattern.clear();
	pattern.add(Triple.create(Node.createVariable("s"), RDFS.label.asNode(), Node.createVariable("o")));
	rels = translate(pattern, "optimizer/filtertests.n3");
	
	NodeRelation langliteral = search("table1", "label_en", rels);
	
	Expr filterint    = new E_Equals(new E_Datatype(new ExprVar("o")), NodeValueNode.makeNode(Node.createURI(XSDDatatype.XSDint.getURI())));
	Expr filterstring = new E_Equals(new E_Datatype(new ExprVar("o")), NodeValueNode.makeNode(Node.createURI(XSDDatatype.XSDstring.getURI())));
	
	assertEquals("DATATYPE(intliteral) = xsd:int should be TRUE",       Expression.TRUE, TransformExprToSQLApplyer.convert(filterint, intvalue));
	assertEquals("DATATYPE(simpleliteral) = xsd:string should be TRUE", Expression.TRUE, TransformExprToSQLApplyer.convert(filterstring, value));
	assertEquals("DATATYPE(langliteral) = xsd:string should be TRUE",   Expression.TRUE, TransformExprToSQLApplyer.convert(filterstring, langliteral));
}
 
Example 16
Project: c4a_data_repository   File: ExprTransformTest2.java   Source Code and License 6 votes vote down vote up
public void testDisjunction()
{
	List<Triple> pattern = new ArrayList<Triple>();
	pattern.add(Triple.create(Node.createVariable("s"), Node.createURI("http://example.org/value"), Node.createVariable("o")));
	NodeRelation[] rels = translate(pattern, "optimizer/filtertests.n3");
	
	NodeRelation intvalue = search("table2", "intvalue", rels);

	Expr disjunction = new E_LogicalOr(new E_Equals(new ExprVar("o"),  NodeValue.makeNode("1", XSDDatatype.XSDint)), new E_Equals(new ExprVar("o"), NodeValue.makeNode("2", XSDDatatype.XSDint)));
	
	Expression result = TransformExprToSQLApplyer.convert(disjunction, intvalue);
	TypedNodeMaker nm = (TypedNodeMaker) intvalue.nodeMaker(Var.alloc("o"));
	Expression e1 = nm.valueMaker().valueExpression("1");
	Expression e2 = nm.valueMaker().valueExpression("2");
	Expression expected = e1.or(e2);
	
	assertEquals("?o = \"1\"^^xsd:int || ?o = \"2\"^^xsd:int", expected, result);
}
 
Example 17
Project: BimSPARQL   File: GeometryConverter.java   Source Code and License 6 votes vote down vote up
public void addMaterialTriples(InstanceGeometry ig){
	if(ig.getColors()!=null&&ig.getColors().length>0){
    String materials=""+ig.getColors()[0];
    for(int i=1;i<ig.getColors().length;i++){
    	materials=materials+" "+ig.getColors()[i];
    }
    
    String indices=""+ig.getMaterialIndices()[0];
    for(int i=1;i<ig.getMaterialIndices().length;i++){
    	indices=indices+" "+ig.getMaterialIndices()[i];
    }
    
    Node hasMaterials=NodeFactory.createURI(GEOM.getURI()+"hasMaterials");
    Node hasMaterialIndices=NodeFactory.createURI(GEOM.getURI()+"hasMaterialIndices");
	getRdfWriter().triple(new Triple(NodeFactory.createURI(baseUri+"Geometry"+"_"+ig.getId()),hasMaterials,NodeFactory.createLiteral(materials)));
	getRdfWriter().triple(new Triple(NodeFactory.createURI(baseUri+"Geometry"+"_"+ig.getId()),hasMaterialIndices,NodeFactory.createLiteral((indices))));
	}
   
}
 
Example 18
Project: BimSPARQL   File: GeometryConverter.java   Source Code and License 6 votes vote down vote up
public void addGeometryTriples(InstanceGeometry ig,boolean boundingbox) throws WktWriteException{
	Geometry g=toGeometry(ig);

   	String s=toWKT(g);
   	getRdfWriter().triple(new Triple(NodeFactory.createURI(baseUri+ig.getType()+"_"+ig.getId()),GEOM.hasGeometry.asNode(),NodeFactory.createURI(baseUri+"Geometry"+"_"+ig.getId())));
   	getRdfWriter().triple(new Triple(NodeFactory.createURI(baseUri+"Geometry"+"_"+ig.getId()),GEOM.asBody.asNode(),NodeFactory.createLiteral((s))));
   	Box mvbb=null;
/*   	if((!g.isEmpty())&&boundingbox){
   		Box aabb=toAABB(g);
   		mvbb=toMVBB(g);
   		if(aabb.getVolume()<mvbb.getVolume()){
   			System.out.println("Error: AABB: "+ aabb.getVolume()+" | MVBB: "+mvbb.getVolume());
   		}
   		getRdfWriter().triple(new Triple(NodeFactory.createURI(baseUri+"Geometry"+"_"+ig.getId()),GEOM.asAABB.asNode(),NodeFactory.createLiteral((toWKT(aabb.toPolyhedralSurface())))));
   		getRdfWriter().triple(new Triple(NodeFactory.createURI(baseUri+"Geometry"+"_"+ig.getId()),GEOM.asMVBB.asNode(),NodeFactory.createLiteral((toWKT(mvbb.toPolyhedralSurface())))));
   	}*/
}
 
Example 19
Project: BimSPARQL   File: HasLowerStoreyPF.java   Source Code and License 6 votes vote down vote up
@Override
protected HashSet<Node> getRelatedSubjects(Node node, ExecutionContext execCxt) {
	HashSet<Node> results = new HashSet<Node>();

	Graph graph = execCxt.getActiveGraph();

	Node clazz = NodeFactory.createURI(Namespace.IFC2X3_TC1 + "IfcBuildingStorey");
	LinkedList<Storey> storeys = new LinkedList<Storey>();
	if (graph.contains(node, RDF.type.asNode(), clazz)) {
		Storey storey = new Storey(node, elevation(node, graph));
		ExtendedIterator<Triple> triples = graph.find(null, RDF.type.asNode(), clazz);
		while (triples.hasNext()) {
			Node subject = triples.next().getSubject();
			Storey s = new Storey(subject, elevation(subject, graph));
			if (s.elevation > storey.elevation) {
				addStorey(storeys, s, graph);
			}
		}
		if (storeys.size() > 0) {
			results.add(storeys.get(0).storey);
		}
	}
	return results;
}
 
Example 20
Project: BimSPARQL   File: HasLowerStoreyPF.java   Source Code and License 6 votes vote down vote up
@Override
protected HashSet<Node> getRelatedObjects(Node node, ExecutionContext execCxt) {
	HashSet<Node> results = new HashSet<Node>();

	Graph graph = execCxt.getActiveGraph();

	Node clazz = NodeFactory.createURI(Namespace.IFC2X3_TC1 + "IfcBuildingStorey");
	LinkedList<Storey> storeys = new LinkedList<Storey>();
	if (graph.contains(node, RDF.type.asNode(), clazz)) {
		Storey storey = new Storey(node, elevation(node, graph));
		ExtendedIterator<Triple> triples = graph.find(null, RDF.type.asNode(), clazz);
		while (triples.hasNext()) {
			Node subject = triples.next().getSubject();
			Storey s = new Storey(subject, elevation(subject, graph));
			if (s.elevation < storey.elevation) {
				addStorey(storeys, s, graph);
			}
		}
		if (storeys.size() > 0) {
			results.add(storeys.get(storeys.size() - 1).storey);
		}
	}
	return results;

}
 
Example 21
Project: BimSPARQL   File: RDFWriter.java   Source Code and License 6 votes vote down vote up
private void fillClassInstanceList(LinkedList<Object> tmpList, OntResource typerange, OntProperty p, Resource r) throws IOException, IfcDataFormatException {
    List<Resource> reslist = new ArrayList<Resource>();
    List<IFCVO> entlist = new ArrayList<IFCVO>();

    // createrequirednumberofresources
    for (int i = 0; i < tmpList.size(); i++) {
        if (IFCVO.class.isInstance(tmpList.get(i))) {
        	Resource r1 = getResource(getBaseURI() + createLocalName(typerange.getLocalName() + "_" + IDcounter), typerange);
            reslist.add(r1);
            IDcounter++;
            entlist.add((IFCVO) tmpList.get(i));
            if (i == 0) {
                getRdfWriter().triple(new Triple(r.asNode(), p.asNode(), r1.asNode()));
            }
        }
    }

    // bindtheproperties
    String listvaluepropURI = getOntNS() + typerange.getLocalName().substring(0, typerange.getLocalName().length() - 5);
    OntResource listrange = ontModel.getOntResource(listvaluepropURI);

    addClassInstanceListProperties(reslist, entlist, listrange);
}
 
Example 22
Project: S2RDF   File: ReorderNoCross.java   Source Code and License 5 votes vote down vote up
private boolean hasSharedVars(int triplePos) {
    Triple triple = inputPattern.get(triplePos);
    ArrayList<String> tripleVars = getVarsOfTriple(triple);
    for (int i=0; i<inputPattern.size(); i++) {
        if (i != triplePos && getSharedVars(getVarsOfTriple(inputPattern.get(i)), tripleVars).size() > 0) {
            return true;
        }
    }
    return false;
}
 
Example 23
Project: S2RDF   File: ReorderNoCross.java   Source Code and License 5 votes vote down vote up
private ArrayList<String> getVarsOfTriple(Triple t) {
    ArrayList<String> vars = new ArrayList<String>();
    Node subject = t.getSubject();
    Node predicate = t.getPredicate();
    Node object = t.getObject();
    if(subject.isVariable())
        vars.add(subject.getName());
    if(predicate.isVariable())
        vars.add(predicate.getName());
    if(object.isVariable())
        vars.add(object.getName());
    return vars;
}
 
Example 24
Project: S2RDF   File: TransformerHelper.java   Source Code and License 5 votes vote down vote up
public static BasicPattern transformPathToBasicPattern(Node subject, String sPath, Node object){
	BasicPattern res= new BasicPattern();
	boolean inverse = checkInverse(sPath);
	if (inverse)
	{
		sPath = sPath.substring(1);
		Node temp = subject;
		subject = object;
		object = temp;
	}
	sPath = TransformerHelper.removeScopes(sPath);
	int operatorPos = TransformerHelper.getPosOfNextOperator(sPath);
	if (operatorPos < 0)
	{
		Triple triple = new Triple(subject, NodeFactory.createURI(sPath.substring(1, sPath.length()-1)), object);
		res.add(triple);
		return res;
	}
   	String leftStringPath = sPath.substring(0, operatorPos);
   	String rightStringPath = sPath.substring(operatorPos+1);
   	Node newConection = NodeFactory.createVariable(getNextVaribleName());
   	BasicPattern leftPattern = transformPathToBasicPattern(subject, leftStringPath, newConection);
   	BasicPattern rightPattern = transformPathToBasicPattern(newConection, rightStringPath, object);
   	res.addAll(leftPattern);
   	res.addAll(rightPattern);
	return res;
}
 
Example 25
Project: S2RDF   File: TripleGroup.java   Source Code and License 5 votes vote down vote up
public void add(Triple triple) {
	if (!searchTripleSamePredicate(triple)) {
		triples.add(triple);
		mapping.putAll(getMappingVarsOfTriple(triple));
	} else {
		crossjoin.add(triple);
		crossJoinMapping.putAll(getMappingVarsOfTriple(triple));
	}

}
 
Example 26
Project: S2RDF   File: TripleGroup.java   Source Code and License 5 votes vote down vote up
public boolean searchTripleSamePredicate(Triple triple) {
	for (int i = 0; i < this.triples.size(); i++) {
		if (triples.get(i).getPredicate().equals(triple.getPredicate())) {
			return true;
		}
	}

	return false;
}
 
Example 27
Project: PRoST   File: TriplePattern.java   Source Code and License 5 votes vote down vote up
public TriplePattern(Triple triple, PrefixMapping prefixes, boolean datasetUsesPrefixes){

		// extract and set the subject
		if(triple.getSubject().isVariable()) {
		  subjectType = ElementType.VARIABLE;
		  subject = triple.getSubject().toString();
		}
		else {
		  subjectType = ElementType.CONSTANT;
		  subject = datasetUsesPrefixes? triple.getSubject().toString(prefixes) : 
		    "<" + triple.getSubject().getURI() + ">";
		  
		}
		
		// extract and set the predicate
		predicateType = ElementType.CONSTANT;
		predicate = triple.getPredicate().toString();
		
		// extract and set the object
		if(triple.getObject().isVariable()) {
		  objectType = ElementType.VARIABLE;
		  object = triple.getObject().toString(prefixes);
		}
		else {
		  objectType = ElementType.CONSTANT;
		  object = datasetUsesPrefixes ? triple.getObject().toString(prefixes) : 
		    "<" + triple.getObject().getURI() + ">";
		}
		
	}
 
Example 28
Project: PRoST   File: PtNode.java   Source Code and License 5 votes vote down vote up
public PtNode(List<Triple> jenaTriples, PrefixMapping prefixes, Stats stats) {
	ArrayList<TriplePattern> triplePatterns = new ArrayList<TriplePattern>();
	this.isPropertyTable = true;
	this.tripleGroup = triplePatterns;
	this.children = new ArrayList<Node>();
	this.projection = Collections.emptyList();
	this.stats = stats;
	for (Triple t : jenaTriples){
	  triplePatterns.add(new TriplePattern(t, prefixes, this.stats.arePrefixesActive()));
	}
	this.setIsComplex();
	
}
 
Example 29
Project: PigSPARQL   File: ReorderNoCross.java   Source Code and License 5 votes vote down vote up
protected boolean hasSharedVars(int triplePos) {
	Triple triple = inputPattern.get(triplePos);
	ArrayList<String> tripleVars = getVarsOfTriple(triple);
	for (int i = 0; i < inputPattern.size(); i++) {
		if (i != triplePos && getSharedVars(getVarsOfTriple(inputPattern.get(i)), tripleVars).size() > 0) {
			return true;
		}
	}
	return false;
}
 
Example 30
Project: PigSPARQL   File: ReorderNoCross.java   Source Code and License 5 votes vote down vote up
protected ArrayList<String> getVarsOfTriple(Triple t) {
	ArrayList<String> vars = new ArrayList<String>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable())
		vars.add(subject.getName());
	if (predicate.isVariable())
		vars.add(predicate.getName());
	if (object.isVariable())
		vars.add(object.getName());
	return vars;
}
 
Example 31
Project: PigSPARQL   File: PigBGPBase.java   Source Code and License 5 votes vote down vote up
/**
 * Cross Product between Triple Patterns.
 * Hopefully we never have to use this.
 * 
 * @param start
 * @return
 */
protected int nextCross(int start) {
	List<Triple> triples = opBGP.getPattern().getList();
	Triple triple = triples.get(start);
	ArrayList<String> tripleVars = getVarsOfTriple(triple);

	String crossArgs = (start == 1 ? "t0" : resultName) + ", t" + start;
	// update resultSchema -> add variables of t(start)
	addToResultSchema(tripleVars);

	/*
	 * Successive Triples which also result in a CROSS can be added to this CROSS
	 */
	ArrayList<String> sharedVars;
	int endIndex = start + 1;
	while (endIndex < triples.size()) {
		triple = triples.get(endIndex);
		tripleVars = getVarsOfTriple(triple);
		sharedVars = getSharedVars(resultSchema, tripleVars);
		if (sharedVars.isEmpty()) {
			// add Triple to this CROSS
			crossArgs += ", t" + endIndex;
			// update resultSchema -> add variables of t(endIndex)
			addToResultSchema(tripleVars);
			endIndex++;
		}
		// Triple has shared variables -> END OF CROSS
		else
			break;
	}

	String cross = resultName + " = CROSS " + crossArgs + " ;";
	joinBlock += cross + "\n";

	return endIndex - 1;
}
 
Example 32
Project: PigSPARQL   File: PigBGP.java   Source Code and License 5 votes vote down vote up
protected ArrayList<String> getVarsOfTriple(Triple t) {
	ArrayList<String> vars = new ArrayList<String>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable())
		vars.add(subject.getName());
	if (predicate.isVariable())
		vars.add(predicate.getName());
	if (object.isVariable())
		vars.add(object.getName());
	return vars;
}
 
Example 33
Project: Sempala   File: ReorderNoCross.java   Source Code and License 5 votes vote down vote up
private boolean hasSharedVars(int triplePos) {
	Triple triple = inputPattern.get(triplePos);
	ArrayList<String> tripleVars = getVarsOfTriple(triple);
	for (int i=0; i<inputPattern.size(); i++) {
		if (i != triplePos && getSharedVars(getVarsOfTriple(inputPattern.get(i)), tripleVars).size() > 0) {
			return true;
		}
	}
	return false;
}
 
Example 34
Project: Sempala   File: ReorderNoCross.java   Source Code and License 5 votes vote down vote up
private ArrayList<String> getVarsOfTriple(Triple t) {
	ArrayList<String> vars = new ArrayList<String>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if(subject.isVariable())
		vars.add(subject.getName());
	if(predicate.isVariable())
		vars.add(predicate.getName());
	if(object.isVariable())
		vars.add(object.getName());
	return vars;
}
 
Example 35
Project: Sempala   File: TripleGroup.java   Source Code and License 5 votes vote down vote up
public void add(Triple triple) {
	if (!searchTripleSamePredicate(triple)) {
		triples.add(triple);
		mapping.putAll(getMappingVarsOfTriple(triple));
	} else {
		crossjoin.add(triple);
		crossJoinMapping.putAll(getMappingVarsOfTriple(triple));
	}
}
 
Example 36
Project: Sempala   File: TripleGroup.java   Source Code and License 5 votes vote down vote up
public boolean searchTripleSamePredicate(Triple triple) {
	for (int i = 0; i < this.triples.size(); i++) {
		if (triples.get(i).getPredicate().equals(triple.getPredicate())) {
			return true;
		}
	}

	return false;
}
 
Example 37
Project: Sempala   File: ImpalaComplexTripleGroup.java   Source Code and License 5 votes vote down vote up
/**
 * From a triple the method extracts the variable names 
 * and the columns to be selected from the table referring to that variable.
 * 
 * @param t
 * @return
 */
private HashMap<String, String[]> getMappingVarsOfTriple(Triple t) {
	HashMap<String, String[]> result = new HashMap<String, String[]>();
	Node subject = t.getSubject();
	Node predicate = t.getPredicate();
	Node object = t.getObject();
	if (subject.isVariable()){
		result.put(subject.getName(),
				new String[] { Tags.SUBJECT_COLUMN_NAME });
	}
	if (predicate.isVariable()) {
		selectFromTripleStore = true;
		result.put(predicate.getName(),
				new String[] { Tags.PREDICATE_COLUMN_NAME });
	}
	if (object.isVariable()) {
		if (selectFromTripleStore) {
			result.put(object.getName(),
					new String[] { Tags.OBJECT_COLUMN_NAME });
		} else {
			String objectString = object.getName();
			String predicateString = getPropertyFromURI(FmtUtils
					.stringForNode(predicate, prefixMapping), false);
			
			
			result.put(objectString, new String[] { SpecialCharFilter
					.filter(predicateString) });
		}
	}
	return result;
}
 
Example 38
Project: KBox   File: TripleResultSet.java   Source Code and License 5 votes vote down vote up
@Override
protected QuerySolutionMap getQuerySolutionMap(Triple t) {
	QuerySolutionMap map = new QuerySolutionMap();
	map.add(SUBJECT, model.asRDFNode(t.getSubject()));
	map.add(PREDICATE, model.asRDFNode(t.getPredicate()));
	map.add(OBJECT, model.asRDFNode(t.getObject()));
	return map;
}
 
Example 39
Project: r2rml-kit   File: JenaGraphExample.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) {
	// Load mapping file
	Model mapModel = FileManager.get().loadModel("doc/example/mapping-iswc.ttl");
	
	// Read mapping file
	D2RQReader reader = new D2RQReader(mapModel, "http://localhost:2020/");
	Mapping mapping = reader.getMapping();
	
	// Compile mapping for D2RQ engine
	CompiledMapping compiled = mapping.compile();

	// Set up the GraphD2RQ
	GraphD2RQ g = new GraphD2RQ(compiled);

	// Create a find(spo) pattern 
	Node subject = Node.ANY;
	Node predicate = DC.date.asNode();
	Node object = Node.createLiteral("2003", null, XSDDatatype.XSDgYear);
	Triple pattern = new Triple(subject, predicate, object);

	// Query the graph
	Iterator<Triple> it = g.find(pattern);
	
	// Output query results
	while (it.hasNext()) {
		Triple t = it.next();
	    System.out.println("Published in 2003: " + t.getSubject());
	}
	g.close();
}
 
Example 40
Project: r2rml-kit   File: TripleRelation.java   Source Code and License 5 votes vote down vote up
public TripleRelation selectTriple(Triple t) {
	NodeRelation r = this;
	r = NodeRelationUtil.select(r, SUBJECT, t.getSubject());
	r = NodeRelationUtil.select(r, PREDICATE, t.getPredicate());
	r = NodeRelationUtil.select(r, OBJECT, t.getObject());
	r = NodeRelationUtil.project(r, SPO);
	if (t.getObject().isConcrete() && !t.getSubject().isConcrete()) {
	    r = new NodeRelation(getSQLConnection(), 
	    		LimitOp.swapLimits(r.getBaseTabular()), r.getBindingMaker());
	}
	return fromNodeRelation(r);
}